var sysMenusModel = $.extend(TradeModel, {
    selRow: null,
    editor:{
        title: '菜单',
        winid:'sysMenus_EditForm',
        formid:'sysMenus_EditForm_fm',
        width:720,
        height:300
    },
    postAction: 'add',
    url: '/contest18/mgr/sysMenus',
    title: '菜单',
    idField: "menuCode",
    titleField: "menuName",
    toolbar: 'sysMenus_Toolbar',
    datagrid: 'sysMenus_Grid'
});

sysMenusModel.reload =function() {
    ReloadList_sysMenus();
    $('#sysMenus_EditForm').dialog('close');
}



function Del_sysMenus() {
    var _checkedItems = $('#sysMenus_Grid').datagrid('getChecked');
    var _delcount=0;
    var _names = '';
    if(_checkedItems.length<1) {
        $.messager.alert('提示', '请选择需要删除的数据!' );
        return;
    }
    $.each(_checkedItems, function(index, item){
        _names +='<br/>'+ item[sysMenusModel.titleField] ;
    });
    var url = sysMenusModel.url+'/del' ;
    $.messager.confirm('确定要删除如下信息?', '' + _names , function (r) {
        if (r) {
            $.each(_checkedItems, function(index, item){
                Onluy.ajax.postData(url,item,  function(result){
                    if (result.rspCode ==0) {
                        _delcount++;
                    }
                },function(error){  } );
            });
            $.messager.alert('提示', '成功删除'+_delcount+'条数据!' );
            ReloadList_sysMenus();
        }
    });
}


function ShowEditForm_sysMenus(type) {
    switch (type) {
        case  1:
            var checkedItems = $('#sysMenus_Grid').datagrid('getChecked');
            if(checkedItems.length>1){
                $.messager.alert('提示', '只能选择一条记录修改!' );
                return;
            }
            sysMenusModel.selRow = checkedItems[0];
            sysMenusModel.postAction = 'update';
            break;

        case  0:
        default:
            sysMenusModel.selRow = {}
            sysMenusModel.postAction = 'add';
            break;
    }

    $('#sysMenus_EditForm_fm').form('clear' );
    $('#sysMenus_EditForm_fm').form('load', sysMenusModel.selRow);
    $('#sysMenus_EditForm').dialog({
        resizable: false,
        maximizable: false
    })
    .dialog('open')
    .dialog('setTitle', (sysMenusModel.postAction == 'add') ? '新增' + sysMenusModel.title : '修改' + sysMenusModel.selRow[sysMenusModel.titleField]);
}
function save_sysMenus() {
    sysMenusModel.submit();
}
function sysMenusSearchbox(value, name) {
    if (value == "" || value == null) {
        $.messager.alert('提示', '请输入关键字!', 'info');
    }
    else {
        $('#sysMenus_Grid').datagrid('reload');
    }
}

function ReloadList_sysMenus() {
    $('#sysMenus_Grid').datagrid('reload');
}



$(function () {
    $('#sysMenus_Grid').datagrid({
        pageSize: 10,
        fit: true,
        checkOnSelect: true,
        selectOnCheck: true,
        singleSelect: false,
        checkbox: true,
        fitColumns: true,
        pagination: true,
        method: 'post',
        url: sysMenusModel.url+'/list',
        loader:sysMenusModel.Loader,
        toolbar: '#sysMenus_Toolbar',
        columns: [
            [
                { field: 'ck',  checkbox: true },
                { field: 'menuIcon', title: '图标', width:40, align: 'left' ,
                    formatter: function (value, rec) {
                        var htmlstr='<img   src="'+value+'"  />';
                        return htmlstr;
                    }},
                { field: 'menuName', title: '名称', width:100, align: 'left'},
                { field: 'menuCode', title: '编码', width:100, align: 'left'},
                { field: 'parentMenuCode', title: '上级编码', width:100, align: 'left'},
                { field: 'sysCode', title: '概要', width:300, align: 'left' ,
                    formatter: function (value, rec) {
                        var htmlstr= '<br/>系统来源:'+rec['sysCode'];
                        htmlstr+=' ，归属地市:'+rec['areaCode'];
                        htmlstr+='， 排序:'+rec['menuSort'];
                        return htmlstr;
                 }}
            ]
        ],


        onLoadError: function (res) {
            sysMenusModel.ShowException(res);
        },
        onLoadSuccess: function (data) {
            if(data.success){ }
            else if(data.msg){
                $.messager.alert('加载错误', data.msg );
            }
        },
        onBeforeLoad: function (param) {
            $('#sysMenus_Grid').datagrid('clearSelections');
            param['catchKey'] = Math.random();
            param['name'] = $('#sysMenus_ss').searchbox('getName');
            param['value'] = $('#sysMenus_ss').searchbox('getValue');
        }
    });
    //设置分页控件
    var p = $('#sysMenus_Grid').datagrid('getPager');
    $(p).pagination({
        pageSize: 10, //每页显示的记录条数，默认为10
        pageList: [10,20, 30, 50, 100], //可以设置每页记录条数的列表
        beforePageText: '第', //页数文本框前显示的汉字
        afterPageText: '页    共 {pages} 页',
        displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录',
        onBeforeRefresh: function () {
            $(this).pagination('loading');
            $(this).pagination('loaded');
        }
    });

});



